package com.irishud.hudnavi.ar_navi;

import android.content.Context;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.LocationSource.OnLocationChangedListener;

import de.greenrobot.event.EventBus;

public class LocationAdapter implements AMapLocationListener {
	public class CityChangedEvent{
		String cityName;
		public CityChangedEvent(String name){
			cityName = name;
		}
		public String getCityName(){
			return cityName;
		}
	}
	// TAG
	private static final String TAG = "NAV:LocationAdapter";
	// 单例
	private AMapLocationClient client = null;
	private AMapLocationClientOption option = null;
	private static LocationAdapter sInstance = null;
	// 定位回调
	public volatile OnLocationChangedListener mListener = null;

	// 构造函数
	public static LocationAdapter InitInstance(Context context) {
		if (sInstance == null) {
            synchronized (LocationAdapter.class) {
                sInstance = new LocationAdapter(context);
                if (sInstance != null) {
                    if (sInstance.client == null) {
                        sInstance.init(context);
                    }
                }
            }
		}
		return sInstance;
	}


	private void init(Context context) {
		currentCityName = "";
		option = new AMapLocationClientOption();
		option.setLocationMode(AMapLocationMode.Hight_Accuracy);// 设置为高精度定位模式
		option.setNeedAddress(true);
		client = new AMapLocationClient(context);
		client.setLocationListener(this);// 设置定位监听
		client.setLocationOption(option);// 设置定位参数
		// 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
		// 注意设置合适的定位时间的间隔（最小间隔支持为2000ms），并且在合适时间调用stopLocation()方法来取消定位请求
		// 在定位结束后，在合适的生命周期调用onDestroy()方法
		// 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除
		client.startLocation();
		Log.d(TAG, "init ok,speak location");
	}

	private void uninit() {
		client.setLocationListener(null);
		client = null;
	}

	// 析构函数
	public static void FreeInstance() {
		if (sInstance != null) {
			sInstance.uninit();
			sInstance.client.stopLocation();
			sInstance.client.onDestroy();
			sInstance.client = null;
			sInstance = null;
		}
	}

	// 构造函数
	private static Context mContext = null;

	public LocationAdapter(Context context) {
		mContext = context;
	}

	// 返回值
	public volatile String currentCityName = "";
	public volatile double lng = 0;
	public volatile double lat = 0;
	public volatile float fSpeed = 0;
	public volatile AMapLocation myCurrentLocation = null;
	// 返回状态
	public volatile boolean locationHasUpdated = false;
	// 更新系统时间标记
	public volatile boolean systemTimeNeedRefresh = true;

	@Override
	public void onLocationChanged(AMapLocation location) {
		if (location != null && location.getErrorCode() == 0) {
			// 地图回调
			EventBus.getDefault().post(location);
			Log.e(TAG,"LOCATION gps ok ============> "+location.getCity());
			// 定位成功
			locationHasUpdated = true;
			lng = location.getLongitude();
			lat = location.getLatitude();
			fSpeed = location.getSpeed();
			if (location.getLocationType() == AMapLocation.LOCATION_TYPE_CELL || location.getLocationType() == AMapLocation.LOCATION_TYPE_FIX_CACHE) {
				if(!currentCityName.equals(location.getCity())){
					// 定位城市发生改变，需要触发离线地图更新检测
					Log.e(TAG,"=========> City changed ============>");
					EventBus.getDefault().post(new CityChangedEvent(location.getCity()));
					currentCityName = location.getCity();
				}
			}
			// currentCityName=arg0.getProvince();
			//Log.d(TAG,"onLocationChanged:city=" + currentCityName + ";lng=" + lng + ";lat=" + lat + ";locationType=" + location.getLocationType());
			myCurrentLocation = location;
			// 更新系统时间
			if (systemTimeNeedRefresh) {
				// SystemClock.setCurrentTimeMillis(location.getTime());
				systemTimeNeedRefresh = false;
			}
		}
	}
}
